package com.crowd.service.impl;

import com.crowd.entity.Auth;
import com.crowd.entity.AuthExample;
import com.crowd.mapper.AuthMapper;
import com.crowd.service.api.IAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Stone
 * @date 2020/5/26 16:46
 */
@Service
public class AuthServiceImpl implements IAuthService {

    @Autowired
    private AuthMapper authMapper;

    /**
     * @Author:Stone

       * @return: java.util.List<com.crowd.entity.Auth>
     * @Date: 2020/5/26 17:12
     * @Description:去全部Auth数据
    */
    @Override
    public List<Auth> getAuthList() {
        return authMapper.selectByExample(new AuthExample());
    }

    /**
     * @Author:Stone
       * @Param roleId:
       * @return: java.util.List<java.lang.Integer>
     * @Date: 2020/5/26 18:58
     * @Description:通过roleId去中间表去已经分配的权限
    */
    @Override
    public List<Integer> getAssignedAuthIdByRoleId(Integer roleId) {
        return authMapper.selectAssignedAuthIdByRoleId(roleId);
    }

    /**
     * @Author:Stone
       * @Param map: key为authIdArray，roleId
       * @return: void
     * @Date: 2020/5/27 16:50
     * @Description:保存新分配的权限
    */
    @Override
    public void saveRoleAuthRelathinship(Map<String, List<Integer>> map) {
        // 1.删除旧权限
        List<Integer> authIdList = map.get("authIdArray");
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        authMapper.deleteOldRoleAuthRelathinship(roleId);
        // 2.保存新权限
        if(authIdList!=null && authIdList.size()>0){
            authMapper.insertNewRoleAuthRelathinship(authIdList,roleId);
        }
    }

    /**
     * @Author:Stone
       * @Param adminId:
       * @return: java.util.List<java.lang.String>
     * @Date: 2020/6/3 21:29
     * @Description: 根据adminId,找到所有权限
    */
    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {
        List<String> authNameList = authMapper.selectAssignedAuthNameByAdminId(adminId);
        return authNameList;
    }
}
